package com.wanke.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanke.bean.CheckDetail;
import com.wanke.bean.ClazzDetail;
import com.wanke.bean.WrongResult;
import com.wanke.entity.Banji;
import com.wanke.entity.Course;
import com.wanke.entity.Student;
import com.wanke.entity.Teacher;
import com.wanke.service.HelloService;

@Controller
public class StudentController {

	@Resource(name = "helloService")
	HelloService helloService;

	@RequestMapping("test")
	public void test() {
		helloService.del();
	}

	@RequestMapping("/login")
	public ModelAndView Login(String username, String password, String type, HttpSession session) {
		ModelAndView mav = new ModelAndView();
		if ("student".equals(type)) {
			System.out.println("------>>>");
			Student stu = helloService.getLogin(username, password);
			if((Integer.parseInt(stu.getType()))!=1){
				mav.setViewName("wrong_user");
				return mav;
			}
			int stu_id = stu.getId();
			session.setAttribute("stu_id", stu_id);
			mav.addObject("loginer", stu.getName());
			mav.setViewName("chick");
			return mav;
		} else if("teacher".equals(type)){
			Teacher teacher=helloService.getTLogin(username, password);
			session.setAttribute("teacher", teacher);
			List<CheckDetail> list=helloService.showToday(teacher.getId());
			List<Banji> bjs=helloService.getBanjiByTeacherId(teacher.getId());
			mav.addObject("lists", list);
			mav.addObject("bjs", bjs);
			mav.addObject("loginer", teacher.getName());
			mav.setViewName("showToTeacher");
			return mav;
		}else{
			mav.setViewName("no_role");
			return mav;
		}
	}

	@RequestMapping("/class_check")
	public ModelAndView classCheck(HttpSession session) {
		ModelAndView mav = new ModelAndView();
		int stu_id = (int) session.getAttribute("stu_id");
		ClazzDetail cd = helloService.getCheck(stu_id);
		if (cd != null) {
			List<Student> studs=helloService.getAllStu();
			mav.setViewName("class_check");
			mav.addObject("cd", cd);
			mav.addObject("studs",studs);
			return mav;
		} else {
			mav.setViewName("wrong_time");
			return mav;
		}
	}
	
	@RequestMapping(value="/docheck",method=RequestMethod.POST)
	public @ResponseBody Map doCheck(String result,String cour_id){
		System.out.println(result);
		String[] results={};
		String[] buf=result.split("&&");
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String today = sdf.format(new Date());
		List<Integer> ccids=helloService.getThisCCId(today);
		if(ccids.size()<buf.length){
			ccids.add(0);
			ccids.add(0);
			ccids.add(0);
		}
	
//		System.out.println(buf[3]);
		for(int i=0;i<buf.length;i++){
			results=buf[i].split(",");
			String res=results[1];
			int stu_id=Integer.parseInt(results[0]);
			helloService.addCheck(res, Integer.parseInt(cour_id), new Date(), stu_id,ccids.get(i));
		}
		Map map=new HashMap();
		return map;
	}
	
	@RequestMapping("/getresult")
	public ModelAndView getResult(){
		
		ModelAndView mav=new ModelAndView();
		List<Course> ccs=helloService.getResult();

		mav.setViewName("today_detail");
		mav.addObject("cs", ccs);
		return mav;
	}
	
	@RequestMapping(value="/checkagain",method=RequestMethod.GET)
	public ModelAndView checkAgain(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		ClazzDetail cd=helloService.checkAgain(Integer.parseInt(request.getParameter("cc_id")));
		List<Student> studs=helloService.getAllStu();
		mav.addObject("cd", cd);
		mav.addObject("studs", studs);
		mav.setViewName("check_again");
//		System.out.println(request.getParameter("cc_id"));
		return mav;
	}
	
	@RequestMapping("/gerenchidao")
	public ModelAndView getGerenChiDao(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		int stu_id=Integer.parseInt(request.getParameter("stu_id"));
		String stu_name=helloService.getStuName(stu_id);
		List<WrongResult> wrs = helloService.getStuChidao(stu_id);
		mav.addObject("wrs", wrs);
		mav.addObject("stu_name", stu_name);
		mav.setViewName("gerenchidao");
		return mav;
	}
	
	@RequestMapping("/gerenqingjia")
	public ModelAndView getGerenQingjia(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		int stu_id=Integer.parseInt(request.getParameter("stu_id"));
		String stu_name=helloService.getStuName(stu_id);
		List<WrongResult> wrs = helloService.getStuQingjia(stu_id);
		mav.addObject("wrs", wrs);
		mav.addObject("stu_name", stu_name);
		mav.setViewName("gerenqingjia");
		return mav;
	}
	
	@RequestMapping("/gerenkuangke")
	public ModelAndView getGerenKuangke(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		int stu_id=Integer.parseInt(request.getParameter("stu_id"));
		String stu_name=helloService.getStuName(stu_id);
		List<WrongResult> wrs = helloService.getStuKuangke(stu_id);
		mav.addObject("wrs", wrs);
		mav.addObject("stu_name", stu_name);
		mav.setViewName("gerenkuangke");
		return mav;
	}
	
	@RequestMapping("/getchidao")
	public ModelAndView getchidao(){
		ModelAndView mav=new ModelAndView();
		List<WrongResult> wrs = helloService.getChidao();
		mav.addObject("wrs", wrs);
		mav.setViewName("todaychidao");
		return mav;
	}
	
	@RequestMapping("/getqingjia")
	public ModelAndView getqingjia(){
		ModelAndView mav=new ModelAndView();
		List<WrongResult> wrs = helloService.getQingjia();
		mav.addObject("wrs", wrs);
		mav.setViewName("todayqingjia");
		return mav;
	}
	
	@RequestMapping("/getkuangke")
	public ModelAndView getkuangke(){
		ModelAndView mav=new ModelAndView();
		List<WrongResult> wrs = helloService.getKuangke();
		mav.addObject("wrs", wrs);
		mav.setViewName("todaykuangke");
		return mav;
	}

	@RequestMapping("/showstu")
	public ModelAndView showStu(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String stu_id=request.getParameter("stu_id");
		Student s=helloService.showStu(Integer.parseInt(stu_id));
		mav.addObject("s", s);
		mav.setViewName("showstu");
		return mav;
	}
}
